﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.IO;

public partial class patriota_MultipleSearch : System.Web.UI.Page
{
    RE_User userbean = null;
    string[] where2 = new string[16];
    protected void Page_Load(object sender, EventArgs e)
    {
        
       if (Session["userid"] == null) Response.Redirect("../default.aspx");
       if (!Page.IsPostBack)
       {
           userbean = (RE_User)Session["userid"];
           DataTable dt=  DB.getDatosPatriota(" and estado=0");
           Session["dt"] = dt;
           gv_datos.DataSource = dt;
           gv_datos.DataBind();
           gv_datos.Enabled = false;

           pintarcontador();

           cb_centro.DataSource = DB.getCentrosVotacion();
           cb_centro.DataTextField = "codigonombre";
           cb_centro.DataValueField = "cv_id2";
           cb_centro.DataBind();

           chkall.Enabled = false;
           
           enableCheckAdmin();
       }
       else {
           userbean = (RE_User)Session["userid"];
       }

    }

    private void enableCheckAdmin()
    {
        userbean = (RE_User)Session["userid"];
        foreach (GridViewRow ROW in gv_datos.Rows)
        {
          
          

            CheckBox chk = (CheckBox)ROW.FindControl("chk_estado");
            if (chk.Checked && userbean.Level==5 )
            {
                chk.Enabled = true;
            }
        }
    }

    protected void pintarcontador(){
        int empadronado = DB.getVotosPatriota("");
        int votos = DB.getVotosPatriota("where Estado=1");
        int pendientes = empadronado - votos;

        lb_empadronado.Text = empadronado.ToString();
        lb_voto.Text = votos.ToString();
        lb_pendiente.Text = pendientes.ToString();

        lb_usuario.Text = userbean.UserID;
        int registradospor = DB.getVotosPatriota("where Estado=1 and usuario= '"+ userbean.UserID+"'");
        lb_votosusuario.Text = registradospor.ToString();
    
    }
    protected void bt_search_Click(object sender, EventArgs e)
    {
        Label1.Text = ""; lerror.Text = "";
        string where = "AND (";
       
        int p = 0; int cont = 0;
        
        if (cb_criterio.SelectedIndex != 0)
        {
          
            string criterio = cb_criterio.SelectedItem.ToString().ToUpper();
            if (criterio.Equals("PADRON") ){
                if (!tb_1.Text.Trim().Equals("")) { if (p == 1) { where += " OR "; } else { p = 1; } where += " Padron=" + tb_1.Text; where2[cont] = " and (Padron=" + tb_1.Text + " )"; cont++; }
                if (!tb_2.Text.Trim().Equals("")) { if (p == 1) { where += " OR "; } else { p = 1; } where += "  Padron=" + tb_2.Text; where2[cont] = " and (Padron=" + tb_2.Text + " )"; cont++; }
                if (!tb_3.Text.Trim().Equals("")) { if (p == 1) { where += " OR "; } else { p = 1; } where += "  Padron=" + tb_3.Text; where2[cont] = " and (Padron=" + tb_3.Text + " )"; cont++; }
                if (!tb_4.Text.Trim().Equals("")) { if (p == 1) { where += " OR "; } else { p = 1; } where += "  Padron=" + tb_4.Text; where2[cont] = " and (Padron=" + tb_4.Text + " )"; cont++; }
                if (!tb_5.Text.Trim().Equals("")) { if (p == 1) { where += " OR "; } else { p = 1; } where += " Padron=" + tb_5.Text; where2[cont] = " and (Padron=" + tb_5.Text + " )"; cont++; }
                if (!tb_6.Text.Trim().Equals("")) { if (p == 1) { where += " OR "; } else { p = 1; } where += "  Padron=" + tb_6.Text; where2[cont] = " and (Padron=" + tb_6.Text + " )"; cont++; }
                if (!tb_7.Text.Trim().Equals("")) { if (p == 1) { where += " OR "; } else { p = 1; } where += "  Padron=" + tb_7.Text; where2[cont] = " and (Padron=" + tb_7.Text + " )"; cont++; }
                if (!tb_8.Text.Trim().Equals("")) { if (p == 1) { where += " OR "; } else { p = 1; } where += "  Padron=" + tb_8.Text; where2[cont] = " and (Padron=" + tb_8.Text + " )"; cont++; }
                if (!tb_9.Text.Trim().Equals("")) { if (p == 1) { where += " OR "; } else { p = 1; } where += "  Padron=" + tb_9.Text; where2[cont] = " and (Padron=" + tb_9.Text + " )"; cont++; }
                if (!tb_10.Text.Trim().Equals("")) { if (p == 1) { where += " OR "; } else { p = 1; } where += " Padron=" + tb_10.Text; where2[cont] = " and (Padron=" + tb_10.Text + " )"; cont++; }
                if (!tb_11.Text.Trim().Equals("")) { if (p == 1) { where += " OR "; } else { p = 1; } where += " Padron=" + tb_11.Text; where2[cont] = " and (Padron=" + tb_11.Text + " )"; cont++; }
                if (!tb_12.Text.Trim().Equals("")) { if (p == 1) { where += " OR "; } else { p = 1; } where += " Padron=" + tb_12.Text; where2[cont] = " and (Padron=" + tb_12.Text + " )"; cont++; }
                if (!tb_13.Text.Trim().Equals("")) { if (p == 1) { where += " OR "; } else { p = 1; } where += " Padron=" + tb_13.Text; where2[cont] = " and (Padron=" + tb_13.Text + " )"; cont++; }
                if (!tb_14.Text.Trim().Equals("")) { if (p == 1) { where += " OR "; } else { p = 1; } where += " Padron=" + tb_14.Text; where2[cont] = " and (Padron=" + tb_14.Text + " )"; cont++; }
                if (!tb_15.Text.Trim().Equals("")) { if (p == 1) { where += " OR "; } else { p = 1; } where += " Padron=" + tb_15.Text; where2[cont] = " and (Padron=" + tb_15.Text + " )"; cont++; }
                if (!tb_16.Text.Trim().Equals("")) { if (p == 1) { where += " OR "; } else { p = 1; } where += " Padron=" + tb_16.Text; where2[cont] = " and (Padron=" + tb_16.Text + " )"; cont++; }           
            }

            if (criterio.Equals("CEDULA") || criterio.Equals("NOMBRE") || criterio.Equals("CUI")) {

                if (!tb_1.Text.Trim().Equals("")) { if (p == 1) { where += " OR "; } else { p = 1; } where += " upper(" + criterio + ") like '%" + tb_1.Text + "%'"; where2[cont] = " and ( upper("+criterio+" )like '%" + tb_1.Text + "%')" ; cont++; }
                if (!tb_2.Text.Trim().Equals("")) { if (p == 1) { where += " OR "; } else { p = 1; } where += " upper(" + criterio + ") like '%" + tb_2.Text + "%'"; where2[cont] = " and ( upper(" + criterio + " )like '%" + tb_2.Text + "%')"; cont++; }
                if (!tb_3.Text.Trim().Equals("")) { if (p == 1) { where += " OR "; } else { p = 1; } where += " upper(" + criterio + ") like '%" + tb_3.Text + "%'"; where2[cont] = " and ( upper(" + criterio + " )like '%" + tb_3.Text + "%')"; cont++; }
                if (!tb_4.Text.Trim().Equals("")) { if (p == 1) { where += " OR "; } else { p = 1; } where += " upper(" + criterio + ") like '%" + tb_4.Text + "%'"; where2[cont] = " and ( upper(" + criterio + " )like '%" + tb_4.Text + "%')"; cont++; }
                if (!tb_5.Text.Trim().Equals("")) { if (p == 1) { where += " OR "; } else { p = 1; } where += " upper(" + criterio + ") like '%" + tb_5.Text + "%'"; where2[cont] = " and ( upper(" + criterio + " )like '%" + tb_5.Text + "%')"; cont++; }
                if (!tb_6.Text.Trim().Equals("")) { if (p == 1) { where += " OR "; } else { p = 1; } where += " upper(" + criterio + ") like '%" + tb_6.Text + "%'"; where2[cont] = " and ( upper(" + criterio + " )like '%" + tb_6.Text + "%')"; cont++; }
                if (!tb_7.Text.Trim().Equals("")) { if (p == 1) { where += " OR "; } else { p = 1; } where += " upper(" + criterio + ") like '%" + tb_7.Text + "%'"; where2[cont] = " and ( upper(" + criterio + " )like '%" + tb_7.Text + "%')"; cont++; }
                if (!tb_8.Text.Trim().Equals("")) { if (p == 1) { where += " OR "; } else { p = 1; } where += " upper(" + criterio + ") like '%" + tb_8.Text + "%'"; where2[cont] = " and ( upper(" + criterio + " )like '%" + tb_8.Text + "%')"; cont++; }
                if (!tb_9.Text.Trim().Equals("")) { if (p == 1) { where += " OR "; } else { p = 1; } where += " upper(" + criterio + ") like '%" + tb_9.Text + "%'"; where2[cont] = " and ( upper(" + criterio + " )like '%" + tb_9.Text + "%')"; cont++; }
                if (!tb_10.Text.Trim().Equals("")) { if (p == 1) { where += " OR "; } else { p = 1; } where += "upper(" + criterio + ") like '%" + tb_10.Text + "%'"; where2[cont] = " and ( upper(" + criterio + " )like '%" + tb_10.Text + "%')"; cont++; }
                if (!tb_11.Text.Trim().Equals("")) { if (p == 1) { where += " OR "; } else { p = 1; } where += "upper(" + criterio + ") like '%" + tb_11.Text + "%'"; where2[cont] = " and ( upper(" + criterio + " )like '%" + tb_11.Text + "%')"; cont++; }
                if (!tb_12.Text.Trim().Equals("")) { if (p == 1) { where += " OR "; } else { p = 1; } where += "upper(" + criterio + ") like '%" + tb_12.Text + "%'"; where2[cont] = " and ( upper(" + criterio + " )like '%" + tb_12.Text + "%')"; cont++; }
                if (!tb_13.Text.Trim().Equals("")) { if (p == 1) { where += " OR "; } else { p = 1; } where += "upper(" + criterio + ") like '%" + tb_13.Text + "%'"; where2[cont] = " and ( upper(" + criterio + " )like '%" + tb_13.Text + "%')"; cont++; }
                if (!tb_14.Text.Trim().Equals("")) { if (p == 1) { where += " OR "; } else { p = 1; } where += "upper(" + criterio + ") like '%" + tb_14.Text + "%'"; where2[cont] = " and ( upper(" + criterio + " )like '%" + tb_14.Text + "%')"; cont++; }
                if (!tb_15.Text.Trim().Equals("")) { if (p == 1) { where += " OR "; } else { p = 1; } where += "upper(" + criterio + ") like '%" + tb_15.Text + "%'"; where2[cont] = " and ( upper(" + criterio + " )like '%" + tb_15.Text + "%')"; cont++; }
                if (!tb_16.Text.Trim().Equals("")) { if (p == 1) { where += " OR "; } else { p = 1; } where += "upper(" + criterio + ") like '%" + tb_16.Text + "%'"; where2[cont] = " and ( upper(" + criterio + " )like '%" + tb_15.Text + "%')"; cont++; }
                
            
            }
            where = where += ")";
            Session["where2"] = where2;
            Session["cont"] = cont;
            DataTable dt = DB.getDatosPatriotaOrdenados(where2, cont);
            Session["dt"] = dt;
            gv_datos.DataSource = dt;
            gv_datos.DataBind();
            gv_datos.Enabled = true;
            chkall.Enabled = true;
            enableCheckAdmin();
            if (where.Equals(""))
            {
                gv_datos.Enabled = false;
            }
            foreach (Control c in form1.Controls)
            {

                if (c is TextBox)
                {
                    ((TextBox)c).Text = "";

                }
            }

            cb_criterio.SelectedIndex = 0;
        }
        else {
            Label1.Text = "Indique un criterio de busqueda."; Label1.BackColor = System.Drawing.Color.Yellow;

        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        lerror.Text = ""; Label1.Text = "";
       

        string where1 = "where ( "; int p = 0;
        string where0 = "where ( "; int p0 = 0; int b0 = 0;
        foreach (GridViewRow ROW in gv_datos.Rows)
        {
            
            
            CheckBox chk = (CheckBox)ROW.FindControl("chk_estado");
            if (chk.Checked && cb_centro.SelectedIndex != 0)
            {
                Label label = (Label)ROW.FindControl("lb_padron");
                if (p == 1) { where1 += " or "; } else { p = 1; }
                where1 += " padron= " + label.Text + " ";
            }
            else if (!chk.Checked){
                Label label = (Label)ROW.FindControl("lb_padron");
                if (p0 == 1) { where0 += " or "; } else { p0 = 1; }
                where0 += " padron= " + label.Text + " ";
            
            }
        }
        where0 += " ) ";
        where1 += " ) ";
        int resp = 0; int resp0 = 0;
        
        if (!where1.Equals("where (  ) ")) { resp = DB.updateEstadoPatriota(where1, 1, cb_centro.SelectedValue.ToString(),userbean.UserID); }
        if (!where0.Equals("where (  ) ")) { resp0 = DB.updateEstadoPatriota(where0, 0, "",""); }
       
       
        if (resp == 1 || resp0==1)
        {
            lerror.Text = "Votos Registrados Exitosamente.";
            int resp1 = DB.sendVotosporCentro();
            pintarcontador();
            cb_centro.SelectedIndex = 0;
            DataTable dt = (DataTable)DB.getDatosPatriota((string)Session["where"]);
            gv_datos.DataSource = dt;
            Session["dt"] = dt;
            where2= (string[])Session["where2"];
            int cont= (int)Session["cont"];
            dt = (DataTable)DB.getDatosPatriotaOrdenados(where2, cont);
            gv_datos.DataSource = dt;
            Session["dt"]= dt;
            gv_datos.DataBind();
            enableCheckAdmin();

        }
        else {
            lerror.Text = "Error. intente guardar los votos otra vez.";
        }

        
    }
    protected void gv_datos_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow ) {
            
        
        }
    }
    protected void chkall_CheckedChanged(object sender, EventArgs e)
    {
        if (chkall.Checked && gv_datos.Enabled)
        {
            foreach (GridViewRow ROW in gv_datos.Rows)
            {
                CheckBox chk = (CheckBox)ROW.FindControl("chk_estado");
                if (chk.Enabled)
                {
                    chk.Checked = true;
                }
            }

        }
        else {
            foreach (GridViewRow ROW in gv_datos.Rows)
            {
                CheckBox chk = (CheckBox)ROW.FindControl("chk_estado");
                if (chk.Enabled)
                {
                    chk.Checked = false;
                }
            }

        
        }
        
        
    }

    protected void gv_datos_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
         DataTable dt=null;
         if (Session["dt"] != null)
         {
             dt = (DataTable)Session["dt"];
         }
        if (dt != null)
        {
            gv_datos.PageIndex = e.NewPageIndex;
            gv_datos.DataSource = dt;
            gv_datos.DataBind();
        }
    }
}